package com.spdg.ring.sqlite.service;

import java.util.List;

import android.content.Context;

import cn.wolf.tools.Log;

import com.spdg.ring.sqlite.dao.MenuDao;
import com.spdg.ring.sqlite.dao.impl.MenuDaoImpl;
import com.spdg.ring.sqlite.model.Menu;

/**
 * <p>Title: menu service     </p>
 * <p>Description: 
 *  provide CURD 
 * </p>
 * <p>@author: Eric.wsd                </p>
 * <p>Copyright: Copyright (c) 2012    </p>
 * <p>Company: HX Co., Ltd.          </p>
 * <p>Create Time: 2012-11-11             </p>
 * <p>Update Time:                     </p>
 * <p>Updater:                         </p>
 * <p>Update Comments:                 </p>
 */
public class MenuService {

	private static MenuService menuService;
	private static MenuDao menuDao;

	private MenuService(Context context) {
		menuDao = MenuDaoImpl.newInstance(context);
	}

	public static MenuService newInstance(Context context) {
		if (menuService == null) {
			menuService = new MenuService(context);
		}

		return menuService;
	}

	/**
	 * get all menu
	 * @return
	 * 		menu list
	 */
	public List<Menu> findAll() {
		return menuDao.find();
	}

	/**
	 * save or update menu, according to whether menu is exist in the DB
	 * @param menu
	 * 		menu object
	 */
	public void saveOrUpdate(Menu menu) {
		if (menu == null) {
			Log.e("menu is null");
			return;
		}

		if (isExist(menu)) {
			menuDao.update(menu);
		} else {
			menuDao.insert(menu);
		}
	}

	/**
	 * whether the menu is exist ,according to file field
	 * @param menu	menu object
	 * @return
	 * 		boolean 
	 */
	public boolean isExist(Menu menu) {
		return menuDao.isExist(menu);
	}

	/**
	 * remove menu, according to menu's file field
	 * @param menu
	 * 		menu obj
	 */
	public void remove(Menu menu) {
		if (menu == null) {
			Log.e("menu is null");
			return;
		}

		menuDao.delete(menu);
	}

	/**
	 * remove all menu
	 */
	public void removeAll() {
		menuDao.deleteAll();
	}

}
